#include <iostream>  
#include <vector>  
#include <limits>  
  
using namespace std;  
  
int minCoins(vector<int>& coins, vector<int>& coinLimits, int amount) {  
    vector<vector<int>> dp(coins.size() + 1, vector<int>(amount + 1, numeric_limits<int>::max()));  
    dp[0][0] = 0;  
  
    for (int i = 1; i <= coins.size(); i++) {  
        int coin = coins[i - 1];  
        int limit = coinLimits[i - 1];  
        for (int j = 0; j <= limit; j++) {  
            for (int k = 0; k <= amount; k++) {  
                if (k >= coin * j && dp[i - 1][k - coin * j] != numeric_limits<int>::max()) {  
                    dp[i][k] = min(dp[i][k], dp[i - 1][k - coin * j] + j);  
                }  
            }  
        }  
    }  
  
    if (dp[coins.size()][amount] == numeric_limits<int>::max()) {  
        return -1; // 无法凑出总金额  
    } else {  
        return dp[coins.size()][amount];  
    }  
}  